import { useModel } from "umi";
import compress from "gql-compress";

const useUpdateGlobalSetting = () => {
    const { graphqlFetch } = useModel("global");

    const updateGlobalSetting = async (variables, operate) => {
        return await graphqlFetch({
            query: operate === "update" ? UpdateGlobalSettingByGsKey : CreateGlobalSetting,
            variables,
        });
    };

    return { updateGlobalSetting };
};

const CreateGlobalSetting = compress(`mutation MyMutation($gsKey: String!, $gsType: String!, $gsValue: String!, $gsJson: JSON!) {
    createGlobalSetting(input: { globalSetting: { gsKey: $gsKey, gsType: $gsType, gsValue: $gsValue, gsJson: $gsJson } }) {
        clientMutationId
    }
}`);

const UpdateGlobalSettingByGsKey = compress(`mutation MyMutation($gsKey: String!, $gsJson: JSON!, $gsType: String!, $gsValue: String!) {
    updateGlobalSettingByGsKey(input: { globalSettingPatch: { gsJson: $gsJson, gsType: $gsType, gsValue: $gsValue }, gsKey: $gsKey }) {
        clientMutationId
    }
}`);

export default useUpdateGlobalSetting;
